import dbMenus from '../../utils/dataset/db_menu';
import { devDebug } from '../../utils/settings';

Page({
  data: {
    act: 0,
    loading: 0,
    refresher: 0,
    refreshing: 0,
    dbName: '',
    dbMenu: [],
    dbData: [],
  },
  onLoad(options) {
    this.setData({ dbMenu: dbMenus[options.dbName], dbName: options.dbName });
    if (!devDebug) this.setData({ refreshing: 1 - this.data.refreshing });
  },
  onChangeTab(ev) {
    this.setData({ act: ev.detail.index });
    // 交由 search 数据监听器处理
    // this.onSearch();
    this.setData({ refreshing: 1 - this.data.refreshing });
  },
  async onSearch(ev) {
    this.setData({ loading: 1 });
    let dbData = null;
    const searchData = this.data.dbMenu.tabs[this.data.act].searchData;
    if (ev && ev.detail) {
      dbData = await this.data.dbMenu.search(ev.detail.searchText, searchData);
    } else {
      dbData = await this.data.dbMenu.search('', searchData);
    }
    console.log(dbData);
    this.setData({ loading: 0, dbData: dbData, refresher: 0 });
  },

  onShow() {

  },

  onPullDownRefresh() {
    // 交由 search 数据监听器处理
    // this.onSearch();
    this.setData({ refreshing: 1 - this.data.refreshing });
  },

  onReachBottom() {
    wx.showToast({
      title: '到底了',
      icon: 'none',
      duration: 1000
    })
  },

})